import { ajax } from 'nc-lightapp-front';

/**
 * @param itemCode 当前操作表单的键
 *        formId 表单id
 *        areaCodeArr 区域id数组
 *        appCode 本节点的appCode
 *        extendParam dataInitAction返回的参数
 * 		  afterEditUrl 自定义接口，适用于地址不同但内部逻辑跟默认一致的接口
 * 功能：调用后台表单控制接口，操作表单中的某个key对关联的key做出控制
 */
let chectFormAction = function(itemCode, fromId, props, areaCodeArr, appCode, extendParam, afterEditUrl) {
	let { form } = props;
	let formDataRows = form.getAllFormValue(fromId).rows;
	let formData = {
		model: {
			rows: formDataRows
		}
	};
	let tempAreaCode = areaCodeArr;
    let arr = []
    tempAreaCode.forEach((areaCode) => {
        if (areaCode.indexOf('add') > -1) {
            areaCode = areaCode.substr(0, areaCode.indexOf('add')-1)
        }
        arr.push(areaCode)
    })
    let paramObj = {
        itemCode,
        formData,
        appCode,
        'areaCodeArr': arr
    };
	// let paramObj = {
	// 	itemCode,
	// 	formData,
	// 	appCode,
	// 	areaCodeArr
	// };
	if (extendParam && extendParam.extendType) {
		paramObj.extendType = extendParam.extendType;
	}
	if (extendParam && extendParam.isExtendPsn) {
		paramObj.isExtendPsn = extendParam.isExtendPsn;
	}
	if (extendParam && extendParam.pk_org) {
		paramObj.pk_org = extendParam.pk_org;
	}
	ajax({
		url: !!afterEditUrl && afterEditUrl.indexOf('/nccloud/') != -1 ? afterEditUrl: '/nccloud/hrcm/common/AfterEditAction.do',
		data: paramObj,
		success: (result) => {
			if (result.success) {
				let data = result.data;
				if (!data || JSON.stringify(data) === '{}') {
					return false;
				}
				let valueMap = data.valueMap
				if (valueMap) {
					for (let key in valueMap) {
						let item = valueMap[key]
						switch (item.value) {
							case 'N':
								item.value = false
								break
							case 'Y':
								item.value = true
								break
							default:break
						}
					}
				}
				setFormAttrAndValue(data.attrMap, valueMap, props, fromId, areaCodeArr);
			}
		}
	});
};
/**
 * @param attrMapObj: 接口返回需要改变字段的属性对象
 *        valueMapObj: 接口返回需要赋值的对象
 *        areaCodeArr: 区域id数组
 * 功能：表单初始化接口返回的属性对象对表单的控制方法
 */
let setFormAttrAndValue = function(attrMapObj, valueMapObj, props, fromId, areaCodeArr) {
	let attrMap, valueMap;
	if (JSON.stringify(attrMapObj) === '{}') {
		attrMap = {};
	} else {
		attrMap = attrMapObj;
	}
	if (JSON.stringify(valueMapObj) === '{}') {
		valueMap = {
			rows: []
		};
	} else {
		valueMap = valueMapObj;
	}
	// attrMap = attrMapObj;
	// valueMap = valueMapObj;
	let { form } = props;
	let meta = props.meta.getMeta();
	let AllMetaItems = [];
	let metaItemMap = {};
	for (let a = 0; a < areaCodeArr.length; a++) {
		let metaItemsByCode = meta[areaCodeArr[a]].items;
		AllMetaItems = AllMetaItems.concat(metaItemsByCode);
	}
	AllMetaItems.forEach((item, index) => {
		metaItemMap[item.attrcode] = item;
	});

	for (let key in attrMap) {
		let map = attrMap[key];
		if (key === 'disabled') {
			form.setFormItemsDisabled(fromId, map);
		} else if (key === 'visible') {
			form.setFormItemsVisible(fromId, map);
		} else if (key === 'required') {
			form.setFormItemsRequired(fromId, map);
		} else {
			for (let attr in map) {
				if (metaItemMap[key]) {
					metaItemMap[key][attr] = map[attr];
				}
			}
		}
	}

	props.meta.setMeta(meta);
	if (valueMap) {
		for (let key in valueMap) {
			let item = valueMap[key]
			switch (item.value) {
				case 'N':
					item.value = false
					break
				case 'Y':
					item.value = true
					break
				default:break
			}
		}
	}
  	// 合同处理中心不写成这种格式出不来合同主体单位的值
	  if (props.formId === "release_card" || props.formId === "finish_card" || props.formId === "signaplly") {
			form.setFormItemsValue(fromId, {rows: valueMap});
	  } else {
		form.setFormItemsValue(fromId, valueMap);
	  }
};
export { chectFormAction, setFormAttrAndValue };
